package com.dhsc.service.impl.ywhc;

import com.dhsc.domain.jkzx.ywhc.HjfzmxVo;
import com.dhsc.domain.jkzx.ywhc.ShkhmxVo;
import com.dhsc.domain.yypt.LayuiTableVo;
import com.dhsc.domain.yypt.ResponseObject;
import com.dhsc.exception.MyException;
import com.dhsc.local.dao.ywhz.IShkhmxDao;
import com.dhsc.service.ywhc.IShkhmxService;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class ShkhmxServiceImpl implements IShkhmxService {

    @Autowired
    private IShkhmxDao shkhmxDao;
    @Override
    public LayuiTableVo queryShkhmxInfo(ShkhmxVo s) {
        LayuiTableVo obj = new LayuiTableVo();
        try{
            PageMethod.startPage(s.getPage(),s.getLimit());
            List<ShkhmxVo> list = shkhmxDao.queryShkhmxInfo(s);
            PageInfo<ShkhmxVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
        }catch(Exception e){
            log.error("查询收函客户明细信息出错", e);
            throw new MyException("查询收函客户明细信息出错",e);
        }
        return obj;
    }

    @Override
    public LayuiTableVo queryZdjkzhmxInfo(ShkhmxVo s) {
        LayuiTableVo obj = new LayuiTableVo();
        try{
            PageMethod.startPage(s.getPage(),s.getLimit());
            List<ShkhmxVo> list = shkhmxDao.queryZdjkzhmxInfo(s);
            PageInfo<ShkhmxVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
        }catch(Exception e){
            log.error("查询重点监控账户信息出错", e);
            throw new MyException("查询重点监控账户信息出错",e);
        }
        return obj;
    }

    @Override
    public LayuiTableVo queryYcjyzhxzcxInfo(ShkhmxVo s) {
        LayuiTableVo obj = new LayuiTableVo();
        try{
            PageMethod.startPage(s.getPage(),s.getLimit());
            List<ShkhmxVo> list = shkhmxDao.queryYcjyzhxzcxInfo(s);
            PageInfo<ShkhmxVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
        }catch(Exception e){
            log.error("查询异常交易账户限制出错", e);
            throw new MyException("查询异常交易账户限制出错",e);
        }
        return obj;
    }

    @Override
    @Transactional("localTransactionManager")
    public ResponseObject saveXzcsxx(ShkhmxVo s) {
        ResponseObject obj = new ResponseObject();
        try{
            if(s.getType().equals("1")){
                //新增
                shkhmxDao.saveXzcsxx(s);
                //修改限制措施标志
                shkhmxDao.updateXzcsbz(s);
                if(s.getXzyy().equals("准重点监控账户")){
                    //修改重点账户标志
                    shkhmxDao.updateZdzhbz(s);
                }
            }else{
                shkhmxDao.updateXzcsxx(s);
            }
            obj.setStatus(200);
        }catch(Exception e){
            log.error("保存交易账户限制出错", e);
            throw new MyException("保存交易账户限制出错",e);
        }
        return obj;
    }

    @Override
    public LayuiTableVo queryHjfxmxcx(HjfzmxVo h) {
        LayuiTableVo obj = new LayuiTableVo();
        try{
            PageMethod.startPage(h.getPage(),h.getLimit());
            List<HjfzmxVo> list = shkhmxDao.queryHjfxmxcx(h);
            PageInfo<HjfzmxVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
        }catch(Exception e){
            log.error("函件类型分值明细查询出错", e);
            throw new MyException("函件类型分值明细查询出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject saveHjfxmx(HjfzmxVo h) {
        ResponseObject obj = new ResponseObject();
        try{
            if(h.getState() == 1){
                //新增
                shkhmxDao.saveHjfxmx(h);
            }else{
                shkhmxDao.updateHjfxmx(h);
            }
            obj.setStatus(200);
        }catch(Exception e){
            log.error("新增/修改函件分值出错", e);
            throw new MyException("新增/修改函件分值出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject deleteHjfxmxcx(HjfzmxVo h) {
        ResponseObject obj = new ResponseObject();
        try{
            shkhmxDao.deleteHjfxmxcx(h);
            obj.setStatus(200);
        }catch(Exception e){
            log.error("删除函件分值出错", e);
            throw new MyException("删除函件分值出错",e);
        }
        return obj;
    }
}
